/* GSManufactureAddAction2.java
 *
 * Project/Product: JZCG-XMBUS
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   peng.ning	2008-9-28	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package com.hitrust.gs.action;

import java.util.HashMap;

import system.LoginContext;

import client.XMLSend;

import com.apep.gw.service.StuationAnalyse;
import com.apep.gw.service.impl.ProductStuationAnalyse;
import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.bean.GSProductBidBean;
import com.hitrust.gs.bean.GSProductTypeBean;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.dao.GSProductBidDao;
import com.hitrust.gs.dao.GSProductTypeDao;
import com.hitrust.gs.form.GSManufactureAddForm2;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSSystemConfigurator;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSManufactureVO;

import framework.core.ConstDefine;
import framework.core.DateUtil;
import framework.core.SrvContext;

public class GSManufactureAddAction2 extends GSBaseAction {

    /*
     * (non-Javadoc)
     * 
     * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
     */
    /*
     * (non-Javadoc)
     * 
     * @see com.hitrust.gs.service.GSBaseAction#execute(com.hitrust.gs.service.GSBaseForm)
     */
    public String execute(GSBaseForm aForm) throws Exception {
        GSManufactureAddForm2 form = (GSManufactureAddForm2) aForm;
        String category = "";
        String category1 = form.getManuftype1();
        String category2 = form.getManuftype2();
        String category3 = form.getManuftype3();
        // 添加产品种类
        if (category2.equals("0")) {
            category = category1;
        } else if (category3.equals("0")) {
            category = category2;
        } else {
            category = category3;
        }
        String manufactureCode = form.getManufacturecode();
        String concentricityid = form.getConcentricityid();
        String manufactureName = form.getManufacturename();
        String providerid = form.getProviderid();
        String unitprice = form.getUnitprice();
        String quantunit = form.getQuantunit();
        String manufacturetype = form.getManufacturetype();
        String manufacturespec = form.getManfuacturespec();
        String qualityassureperiod = form.getQualityassureperiod();
        String manufacturername = form.getManufacturername();
        String makearea = form.getMakearea();
        String manufacturebrand = form.getManufacturebrand();
        String drawingpiececode = form.getDrawingpiececode();
        String isbuy = form.getIsbuy();
        String machinetype = form.getMachinetype();
        String property = form.getProperty();
        String remark = form.getRemark();
        String mode = form.getBalancemode();
        String planprice = form.getPlanprice();
        String productType = form.getProductType();
        String productBid = form.getProductBid();
        String balancecustomercode=form.getBalancecustomercode();
        
        SrvContext context = SrvContext.getContext(aForm.getRequest());
        LoginContext loginInfo = LoginContext.get(context);
        /** 设置产品相关属性 开始 */
        GSManufactureVO vo = new GSManufactureVO();
        vo.setGs_manufacture_id(manufactureCode);
        vo.setGs_concentricity_id(concentricityid);
        vo.setGs_manufacture_name(manufactureName);
        vo.setGs_manufacture_category_id(category);
        vo.setGs_manufacture_brand(manufacturebrand);
        vo.setGs_manufacture_type(manufacturetype);
        vo.setGs_manufacture_spec(manufacturespec);
        vo.setGs_manufacture_unitprice(unitprice);
        vo.setGs_quant_unit(quantunit);
        vo.setGs_quality_assure_period(qualityassureperiod);
        vo.setGs_manufacturer_name(manufacturername);
        vo.setGs_make_area(makearea);
        vo.setGs_provider_id(providerid);
        vo.setGs_adapt_machine_type(machinetype);
        vo.setGs_recorder_id(loginInfo.getUser().getCode());
        vo.setGs_manufacture_record_date(new DateUtil().getFullString());
        vo.setGs_is_tradeflat_manufacture("1");
        vo.setGs_appender_property(property);
        vo.setGs_remark(remark);
        vo.setGsValidity("1");
        vo.setGs_drawing_peice_code(drawingpiececode);
        vo.setIs_buy(isbuy);
        vo.setGs_balance_mode(mode);
        vo.setCreate_user_id(loginInfo.getLoginUserId());
        vo.setCreate_user(loginInfo.getLoginUserName());
        vo.setCreate_time(new DateUtil().getFullString());
        vo.setLast_update_time(new DateUtil().getFullString());
        vo.setLast_update_user_id(loginInfo.getLoginUserId());
        vo.setLast_update_user(loginInfo.getLoginUserName());
        vo.setBalance_customer_code("0");
        vo.setPic(" ");
        vo.setRecent_adjustprice_date(new DateUtil().getFullString());
        vo.setRecent_adjusted_price(unitprice);
        vo.setIs_normal(GSBaseConstants.IS_NORMAL);
        vo.setWarehouse_code(ConstDefine.T_WARE_HOUSE_CODE);
        vo.setWarehouse_name(ConstDefine.T_WAREE_HOUSE_NAME);
        vo.setIsupdate(GSBaseConstants.IS_UPDATE);// 更新
        vo.setKm_quality_assure_period(form.getKmqualityassureperiod());
        vo.setBalance_customer_code(balancecustomercode);
        if(GSBaseConstants.PLANPRICE.equals("false")){
            planprice=unitprice;
        }

        vo.setGs_planprice(planprice);
        /** 设置产品相关属性 结束 */

        GSManufactureProcessor processor = new GSManufactureProcessor();
        StuationAnalyse productStuationAnalyse = new ProductStuationAnalyse();
        
        boolean bl = false;
        try {
            // 判断是否存在相同集中编码的材料信息
            if (concentricityid.trim().length() != GSBaseConstants.PRODUCT_CONCENTRICITY&&Integer.parseInt(GSSystemConfigurator.getParam("gs.is.concentricity"))==0) {
            	//ZT改
            	String endStr = concentricityid.trim().substring(concentricityid.trim().length()-1);
            	if(concentricityid.trim().length() == GSBaseConstants.PRODUCT_CONCENTRICITY2&&endStr.equals("0")){
            	}else{
            		throw new GSException("集中编码长度必须为" + GSBaseConstants.PRODUCT_CONCENTRICITY + "编码,集中编码："
            				+ concentricityid);
            	}
            }
            GSManufactureBean mbean = processor.getManufInfoByConcentricityId(concentricityid);
            if (mbean != null) {
                throw new GSException("已存在相同集中编码的产品信息,集中编码：" + concentricityid);
            }
            bl = processor.saveManufactureKM(vo);
            
            mbean = processor.getManufInfoByConcentricityId(concentricityid);
            if(mbean!=null){
            	productStuationAnalyse.insert(processor.appendInsertContent(vo), vo.getCreate_time(), loginInfo.getLoginUserId(), loginInfo.getLoginUserName(), mbean.getId());   
            }
            
            GSProductTypeDao typeDao = new GSProductTypeDao();
            GSProductBidDao bidDao = new GSProductBidDao();
            
            GSProductTypeBean bean = typeDao.findProductTypeByProuductCode(vo.getGs_concentricity_id().trim());
            if(bean !=null){
            	bean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
            	bean.setProductCode(vo.getGs_concentricity_id().trim());
            	bean.setProviderCode(vo.getGs_provider_id());
            	bean.setProductType(Integer.parseInt(productType));
            	bean.setIsBid(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
            	bean.setLastUpdateTime(new DateUtil().getFullString());
            	bean.setLastUpdateUser(loginInfo.getLoginUserName());
            	bean.setLastUpdateUserId(loginInfo.getLoginUserId());
            	typeDao.update(bean);
            }else{
              	bean = new GSProductTypeBean();
              	bean.setCreateTime(new DateUtil().getFullString());
              	bean.setCreateUser(loginInfo.getLoginUserName());
              	bean.setCreateUserId(loginInfo.getLoginUserId());
              	bean.setIsBalance(1);
              	bean.setIsBid(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
              	bean.setLastUpdateTime(new DateUtil().getFullString());
              	bean.setLastUpdateUser(loginInfo.getLoginUserName());
              	bean.setLastUpdateUserId(loginInfo.getLoginUserId());
              	bean.setProductCode(vo.getGs_concentricity_id().trim());
              	bean.setProductType(Integer.parseInt(productType));
              	bean.setProviderCode(vo.getGs_provider_id());
              	bean.setProductId(mbean.getId());
              	bean.setRemark("");
              	bean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
              	typeDao.insert(bean);
            }

            	GSProductBidBean bidBean = bidDao.findProductBidByProuductCode(vo.getGs_concentricity_id().trim());
                if(bidBean !=null){
                	bidBean.setProductId(mbean.getId());
                	bidBean.setProductCode(vo.getGs_concentricity_id().trim());
                	bidBean.setProviderCode(vo.getGs_provider_id());
                	bidBean.setProductType(Integer.parseInt(productType));
                	bidBean.setLastUpdateTime(new DateUtil().getFullString());
                	bidBean.setLastUpdateUser(loginInfo.getLoginUserName());
                	bidBean.setLastUpdateUserId(loginInfo.getLoginUserId());
                	bidDao.update(bidBean);
                }else{
        	      	bidBean = new GSProductBidBean();
        	      	bidBean.setCreateTime(new DateUtil().getFullString());
        	      	bidBean.setCreateUser(loginInfo.getLoginUserName());
        	      	bidBean.setCreateUserId(loginInfo.getLoginUserId());
        	      	bidBean.setProductName(vo.getGs_manufacture_name());
        	      	bidBean.setLastUpdateTime(new DateUtil().getFullString());
        	      	bidBean.setLastUpdateUser(loginInfo.getLoginUserName());
        	      	bidBean.setLastUpdateUserId(loginInfo.getLoginUserId());
        	      	bidBean.setProductCode(vo.getGs_concentricity_id().trim());
        	      	bidBean.setProductType(productBid.equals("")?GSBaseConstants.PRODUCT_TYPE_MANAGER_ORTHER_BID:Integer.parseInt(productBid));
        	      	bidBean.setProviderCode(vo.getGs_provider_id().trim());
        	      	bidBean.setProductId(mbean.getId());
        	      	bidBean.setRemark("");
        	      	bidBean.setStatus(Integer.parseInt(ConstDefine.STR_VALID_YES));
        	      	bidDao.insert(bidBean);
                }
    
            
        } catch (GSException err) {
            if (err.getCode() != null) {
                HashMap tMap = new HashMap();
                tMap.put("err", err.getMessage());
                aForm.getRequest().setAttribute("target", "_self");
                aForm.getRequest().setAttribute("message", err.getMessage());
                aForm.getRequest().setAttribute("action", "A400313.do");
                throw err;
            }
            aForm.getRequest().setAttribute("message", "产品信息添加失败");
        }
        if (bl) {
            aForm.getRequest().setAttribute("message", "产品信息添加成功，返回继续添加。");
            // 设置xml参数
            if (XMLSend.getInstance().isToRong()) {
                String actionName = "SubmitMaterialInfo";
                String dataName = "MaterialInfo";
                // 传输xml to 荣爱
                XMLSend.getInstance().submitMaterialInfoSendXML(actionName, dataName, vo);

            }
        } else {
            aForm.getRequest().setAttribute("message", "产品信息添加失败");
        }
        return GSBaseConstants.FORWARD_SUCCESS;
    }
}
